﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using System;
using System.Diagnostics;
using System.Text;

namespace ToPDF.Data
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ToPdfController : ControllerBase
    {

        private readonly PdfHelp pdfHelp;

        public ToPdfController(PdfHelp pdfHelp)
        {
            this.pdfHelp = pdfHelp;
        }

        [HttpPost]
        public async Task<string> ConvertHtmlToPdf([FromForm] string htmlContent)
        {
            if (string.IsNullOrEmpty(htmlContent))
            {
                throw new ArgumentException("Html content cannot be null or empty.");
            }

            // 将htmlContent写入HTML文件中
            string isYes = await pdfHelp.WriteHtmlToFile(htmlContent);
            if (!string.IsNullOrEmpty(isYes))
            {
                string pdfBase64 = await Task.Run(() => pdfHelp.ConvertHtmlContentToPdf(isYes));
                return pdfBase64;
            }


            return "未插入文件";
        }

    }
}
